﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using Skynet.CommonLibrary.WCF;
using Skynet.CommonLibrary.Extension.Log;

namespace SQLQueryUpdateService
{
    public partial class Service : ServiceBase
    {
        ServiceHost _dbHost = null;
        public Service()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
          
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            try
            {
                _dbHost = new ServiceHost(typeof(DataAccessCenter));
                _dbHost.Open();
                WriteAddress(_dbHost);
                LogService.GlobalInfoMessage("服务已正常启用");
            }
            catch (Exception ex)
            {
                LogService.GlobalInfoMessage("DataAccessCenter 绑定错误,详细："+ex.Message);
            }
        }

        protected override void OnStop()
        {
            if (_dbHost != null)
            {
                _dbHost.Close();
            }
        }

        static void WriteAddress(ServiceHost host)
        {
            foreach (ServiceEndpoint endpoint in host.Description.Endpoints)
            {
                LogService.GlobalInfoMessage(host.Description.Name + string.Format("\n\tAddress: {0}\n\tBinding: {1}\n\tContract: {2}" ,endpoint.Address, endpoint.Binding, endpoint.Contract.Name));
            }
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            LogService.GlobalInfoMessage(e.ExceptionObject.ToString());
        }
    }
}
